const Controller = require('egg').Controller;


class ChatController extends Controller {

    async login() {
        const { ctx } = this;
        const { userName, pwd } = ctx.args[0];
        const user = await ctx.model.User.findOneAndUpdate({
            account: userName, password: pwd
        }, {
                online: true
            })
        //console.log(user);
        let msg = {}
        if (user != null) {
            msg = {
                load: "suc",
                user: user
            }
        } else {
            msg.load = "err"
        }
        this.ctx.socket.emit('res', msg);

    }

    async chat() {
        const { ctx } = this
        const { user, touser } = ctx.args[0];
        const data = await ctx.model.Chat.find({
            $or: [{
                user: user,
                touser: touser
            }, {
                user: touser,
                touser: user
            }],
        })
        const rooms = [ctx.room[user], ctx.room[touser]]
        //给对话双方发送消息
        for (let room of rooms) {
            this.ctx.socket.join(room)
            await this.app.io.of("/").to(room).emit('message', {
                fromuser: user,
                touser: touser,
                data: data
            })
        }

    }

    /**在线名单 */
    async online() {
        const { ctx } = this;
        const room = ctx.room["room"]
        const user = await ctx.model.User.find({
            online: true
        })
        await this.app.io.of("/").to(room).emit('online', user);
    }

    /**提醒新消息 */
    async remindNew() {
        const { ctx } = this
        const { user, touser, roomid } = ctx.args[0];
        let data
        if (touser) {
            data = {
                fromuser: user,
                touser: touser
            }
        } else if (roomid) {
            data = {
                roomid: roomid
            }
        }
        let room = ctx.room[touser]
        this.ctx.socket.join(room)
        await this.app.io.of("/").to(room).emit('remindNew', data)
    }

    async chatRoomList() {
        const { ctx } = this
        const { user } = ctx.args[0];

        const data = await ctx.model.Room.find({
            $or: [
                { user: user },
                { tousers: { $all: [user] } }
            ]
        })
        let room = ctx.room[user]
        this.ctx.socket.join(room)
        await this.app.io.of("/").to(room).emit('chatRoomList', data)
    }

    async chatroommsg() {
        const { ctx } = this
        const { roomid } = ctx.args[0];

        const data = await ctx.model.Room.findById(roomid)
        const ret = await ctx.model.Roommsg.find({
            room: roomid
        }).populate('room')
        //console.log(data);

        let tousers = data.tousers
        for (var i = 0, len = tousers.length; i < len; i++) {
            let room = ctx.room[tousers[i]]
            this.ctx.socket.join(room)
            await this.app.io.of("/").to(room).emit('message', {
                tousers: tousers,
                roomid: roomid,
                data: ret
            })
        }
    }




    /**登出 */
    async logout() {
        //console.log('3');
        const { ctx } = this;
        const { userName } = ctx.args[0];
        const user = await ctx.model.User.findOneAndUpdate({
            account: userName
        }, {
                online: false
            })

        await this.online()
    }



    async disconnect() {
        console.log('失去连接');

    }
}


module.exports = ChatController